package com.zysf.library_boot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zysf.library_boot.entity.Menu;
import com.zysf.library_boot.service.MenuService;
import com.zysf.library_boot.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/menus")
public class MenuController {
    @Autowired
    MenuService menuService;

    @RequestMapping("/getMenus")
    public Result getMenus(){
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq("pid",0);
        //父级菜单
        List<Menu> father_menus = menuService.list(qw);
        //所有菜单
        List<Menu> menus = menuService.list();
        for (Menu menu: father_menus){
            List<Menu> son_menus = new ArrayList<>();
            for (Menu all_menu: menus){
                if (all_menu.getPid().equals(menu.getId())){
                    son_menus.add(all_menu);
                }
            }
            menu.setSon_menus(son_menus);
        }
        return Result.ok(father_menus);
    }
}
